package cn.yuli.zookeeper.master;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class Demo implements Watcher {
    private static CountDownLatch lath = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        ZooKeeper zookeeper = new ZooKeeper("39.108.170.73:2181", 5000, new Demo());
        System.out.println(zookeeper.getState());
        lath.await();
        zookeeper.getChildren("/lock",true);
        Thread.sleep(Integer.MAX_VALUE);
    }

    public void process(WatchedEvent event) {
    	System.out.println("发生事件拉");
        if (KeeperState.SyncConnected == event.getState()) {
        	lath.countDown();
        }else if(EventType.NodeChildrenChanged == event.getType()){
        	System.out.println("该节点被改变："+event.getPath());
        }
    }
}

